package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOpUserDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.IssueCompany;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.organization.util.IssueFeePolicyLimit;
import com.turing.post.organization.util.IssueFeePolicyLimitBusiness;
import com.turing.post.organization.util.IssueFeePolicyLimitDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "organization.impl.IssueFeePolicyLimitBusinessImpl")
@Remote
@Local
public class IssueFeePolicyLimitBusinessImpl implements
		IssueFeePolicyLimitBusiness {

	@EJB(mappedName = "organization.impl.IssueFeePolicyLimitDaoImpl")
	private IssueFeePolicyLimitDao issueFeePolicyLimitDaoImpl;

	@EJB(mappedName = "common.impl.SysOpUserDaoImpl")
	private SysOpUserDao sysOpUserDaoImpl;

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao issueCompanyDaoImpl;

	/**
	 * 异常抛出方法
	 * 
	 * @param error
	 * @throws PCPSApplicationException
	 */
	private void errException(String error) throws PCPSApplicationException {
		throw new OrganApplicationException(
				OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
				OrganRespCode.RC_ARGS_NULL, error);
	}

	/**
	 * 记录数据库日志
	 * 
	 * @param webInput
	 * @param logName
	 * @param obj
	 * @param operType
	 * @param lastFieldName
	 * @throws PCPSApplicationException
	 */
	private void insertRecordingOperationLog(PCPSWebInput webInput,
			String logName, IssueCompany obj, String operType,
			String lastFieldName) throws PCPSApplicationException {
		String logRemark = PCPSUtil.RecordingOperationLog(obj, lastFieldName);
		if (!PCPSUtil.isEmpty(logRemark)) {
			// 插入数据库日志
			SysOperLog sysOperLog = new SysOperLog();
			if (webInput != null && webInput.getOperId() != null) {
				sysOperLog.setOperId(webInput.getOperId());
				sysOperLog.setOperType(operType);
				sysOperLog.setLogName(logName);
				if (webInput != null) {
					sysOperLog.setIpAddress(webInput.getIpAddress());
				}
				if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
					sysOperLog.setIssueCompanyNo(webInput.getOperBranchNo()
							.substring(0, 6));
					sysOperLog.setBranchNo(webInput.getOperBranchNo());
				}
				sysOperLog.setRemark(logRemark);
				sysOperLogDaoImpl.insertSysOperLog(sysOperLog);
			}
		}
	}

	/**
	 * 新增发卡商资费上下限信息
	 * 
	 * @param entity
	 */
	@Override
	public void addIssueFeePolicyLimit(PCPSWebInput webInput,
			IssueFeePolicyLimit entity) throws PCPSApplicationException {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			errException("平台管理员不能维护发卡商资费上下限！");
		}
		if (PCPSUtil.isEmpty(entity.getBusinessType())) {
			errException("业务类型未选定！");
		}
		String issueCompnyNo = webInput.getOperBranchNo().substring(0, 6);
		IssueFeePolicyLimit obj = issueFeePolicyLimitDaoImpl
				.getIssueFeePolicyLimitByCompNoAndType(issueCompnyNo, entity
						.getBusinessType());
		if (obj != null) {
			errException("要添加的发卡商资费上下限信息已存在！");
		} else {
			obj = new IssueFeePolicyLimit();
		}
		IssueCompany issueCompany = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(issueCompnyNo);
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			errException("平台管理员不能维护发卡商资费上下限！");
		}
		if (sysOpUserDaoImpl.getSysOpUserByOperId(webInput.getOperId()) == null) {
			errException("操作人员不存在！");
		}
		if (PCPSUtil.isEmpty(entity.getBusinessType())) {
			errException("业务类型未选定！");
		}
		if (entity.getFeeLowerLimit().compareTo(BigInteger.ZERO) == -1) {
			errException("发卡商资费下限必须大于或等于0！");
		}
		if (entity.getFeeUpperLimit().compareTo(BigInteger.ZERO) == -1) {
			errException("发卡商资费上线必须大于或等于0！");
		}
		if (entity.getFeeUpperLimit().compareTo(entity.getFeeLowerLimit()) == -1) {
			errException("发卡商资费上线必须大于或等于其资费下限！");
		}
		obj.setIssueCompanyNo(issueCompnyNo);
		obj.setBusinessType(entity.getBusinessType());
		obj.setFeeLowerLimit(entity.getFeeLowerLimit());
		obj.setFeeUpperLimit(entity.getFeeUpperLimit());
		obj.setOperID(webInput.getOperId());
		obj.setModifyTime(commonDaoImpl.getDatabaseSysTime());
		obj.setRemark(entity.getRemark());
		obj.setStatus(IssueFeePolicyLimit.IFPOLICY_STATUS_EFFECTIVEAUDIT);
		issueFeePolicyLimitDaoImpl.addIssueFeePolicyLimit(obj);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "添加发卡商卡资费上下限", issueCompany,
				SysOperLog.operType_Edit, "IssueCompanyName:"
						+ issueCompany.getIssueCompanyName());
	}

	/**
	 * 根据发卡商号和业务类型获取资费上下限信息
	 * 
	 * @param issueCompnyNoaddIssueFeePolicyLimit
	 * @param businessType
	 * @return
	 */
	@Override
	public IssueFeePolicyLimit getIssueFeePolicyLimitByCompNoAndType(
			PCPSWebInput webInput, String businessType)
			throws PCPSApplicationException {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			errException("平台管理员不能维护发卡商资费上下限！");
		}
		if (PCPSUtil.isEmpty(businessType)) {
			errException("业务类型未选定！");
		}
		return issueFeePolicyLimitDaoImpl
				.getIssueFeePolicyLimitByCompNoAndType(webInput
						.getOperBranchNo().substring(0, 6), businessType);
	}

	/**
	 * 根据发卡商号获取发卡商各业务资费上下限列表
	 * 
	 * @param issueCompnyNo
	 * @return
	 */
	@Override
	public List<IssueFeePolicyLimit> getIssueFeePolicyLimitListByCompNo(
			PCPSWebInput webInput, String businessType, Integer page) throws PCPSApplicationException {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			errException("平台管理员不能维护发卡商资费上下限！");
		}
		return issueFeePolicyLimitDaoImpl
				.getIssueFeePolicyLimitListByCompNo(webInput.getOperBranchNo()
						.substring(0, 6), businessType, page);
	}

	/**
	 * 修改发卡商资费上下限信息
	 * 
	 * @param entity
	 */
	@Override
	public void modifyIssueFeePolicyLimit(PCPSWebInput webInput,
			IssueFeePolicyLimit entity) throws PCPSApplicationException {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			errException("平台管理员不能维护发卡商资费上下限！");
		}
		if (PCPSUtil.isEmpty(entity.getBusinessType())) {
			errException("业务类型未选定！");
		}
		String issueCompnyNo = webInput.getOperBranchNo().substring(0, 6);
		IssueFeePolicyLimit obj = issueFeePolicyLimitDaoImpl
				.getIssueFeePolicyLimitForUpdate(issueCompnyNo, entity
						.getBusinessType());
		if (obj == null) {
			errException("要修改的发卡商资费上下限信息不存在！");
		}
		IssueCompany issueCompany = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(issueCompnyNo);
		if (sysOpUserDaoImpl.getSysOpUserByOperId(webInput.getOperId()) == null) {
			errException("操作人员不存在！");
		}
		if (obj.getFeeLowerLimit().compareTo(BigInteger.ZERO) == -1) {
			errException("发卡商资费下限必须大于或等于0！");
		}
		if (obj.getFeeUpperLimit().compareTo(BigInteger.ZERO) == -1) {
			errException("发卡商资费上线必须大于或等于0！");
		}
		if (obj.getFeeUpperLimit().compareTo(obj.getFeeLowerLimit()) == -1) {
			errException("发卡商资费上线必须大于或等于其资费下限！");
		}
		
		obj.setIssueCompanyNo(issueCompnyNo);
		obj.setBusinessType(entity.getBusinessType());
		obj.setFeeLowerLimit(entity.getFeeLowerLimit());
		obj.setFeeUpperLimit(entity.getFeeUpperLimit());
		obj.setOperID(webInput.getOperId());
		obj.setModifyTime(commonDaoImpl.getDatabaseSysTime());
		obj.setRemark(entity.getRemark());
		obj.setStatus(IssueFeePolicyLimit.IFPOLICY_STATUS_EFFECTIVEAUDIT);
		issueFeePolicyLimitDaoImpl.modifyIssueFeePolicyLimit(obj);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "修改发卡商卡资费上下限", issueCompany,
				SysOperLog.operType_Edit, "IssueCompanyName:"
						+ issueCompany.getIssueCompanyName());
	}

	/**
	 * 总记录数
	 * 
	 * @param issueCompnyNo
	 * @return
	 */
	@Override
	public BigInteger getIssueFeePolicyLimitListTotal(PCPSWebInput webInput, String businessType)
			throws PCPSApplicationException {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			errException("平台管理员不能维护发卡商资费上下限！");
		}
		return issueFeePolicyLimitDaoImpl
				.getIssueFeePolicyLimitListTotal(webInput.getOperBranchNo()
						.substring(0, 6), businessType);
	}

	@Override
	public void authIssueFeePolicyLimitEffective(PCPSWebInput webInput,
			String businessType, String authRemark) throws PCPSApplicationException {
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			errException("平台管理员不能维护发卡商资费上下限！");
		}
		if (PCPSUtil.isEmpty(businessType)) {
			errException("业务类型未选定！");
		}
		String issueCompnyNo = webInput.getOperBranchNo().substring(0, 6);
		IssueFeePolicyLimit obj = issueFeePolicyLimitDaoImpl
				.getIssueFeePolicyLimitForUpdate(issueCompnyNo, businessType);
		if (obj == null) {
			errException("要修改的发卡商资费上下限信息不存在！");
		}

		if (sysOpUserDaoImpl.getSysOpUserByOperId(webInput.getOperId()) == null) {
			errException("审核人员不存在！");
		}
		if(webInput.getOperId().equals(obj.getOperID())){
			errException("审核人员和最后修改人不能是同一个用户！");
		}
		IssueCompany issueCompany = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(issueCompnyNo);

		obj.setStatus(IssueFeePolicyLimit.IFPOLICY_STATUS_EFFECTIVE);
		obj.setLastAuthOperId(webInput.getOperId());
		obj.setLastAuthTime(commonDaoImpl.getDatabaseSysTime());
		obj.setAuthRemark(authRemark);
		
		issueFeePolicyLimitDaoImpl.modifyIssueFeePolicyLimit(obj);

		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "发卡商卡资费上下限生效审核", issueCompany,
				SysOperLog.operType_Edit, "IssueCompanyName:"
						+ issueCompany.getIssueCompanyName());
		
	}

}
